﻿namespace ScrumLite.WebUx.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using ScrumLite.Services;
    using ScrumLite.Services.Contracts;
    using System.Collections.ObjectModel;

    public class MockProjectsService : IProjectsService
    {
        public bool AddProjectCalled { get; set; }
        public bool UpdateProjectCalled { get; set; }
        public Project ProjectAdded { get; set; }
        public IProfileService Profile { get; set; }

        private List<Project> projects = new List<Project>();
        List<ProjectRole> internalProjectRoles = new List<ProjectRole>();
        List<ProjectRole> externalProjectRoles = new List<ProjectRole>();
        Dictionary<Guid, List<TeamMember>> teams = new Dictionary<Guid, List<TeamMember>>();

        public MockProjectsService()
        {            
            projects.Add(new Project { Id = new Guid("A36F736D-5C0C-4DB8-B608-4D144F2205D8"), Name = "Alphamax-v1", StartsOn = DateTime.Now.Date });
            projects.Add(new Project { Id = new Guid("12184E41-B84D-4C2F-9251-CB0F8E8D7DDB"), Name = "Colorado-v1", StartsOn = DateTime.Now.Date });
            projects.Add(new Project { Id = new Guid("185E58A8-279B-4B8E-9590-3302A88858BF"), Name = "Betamax-v1", StartsOn = DateTime.Now.Date.AddMonths(-1) });
            projects.Add(new Project { Id = new Guid("41E98E4C-681A-4988-8F11-4292C8E868E4"), Name = "Blackbull-v2", StartsOn = DateTime.Now.Date.AddMonths(-1) });
            projects.Add(new Project { Id = new Guid("2CC6669E-085D-4B62-9BC5-A0379EC65CBE"), Name = "Calypso-v2", StartsOn = DateTime.Now.Date.AddMonths(-1) });
            projects.Add(new Project { Id = new Guid("9F30E28E-A9C3-4DFE-AD8E-44C7F22E9A7E"), Name = "Ceibo-v1", StartsOn = DateTime.Now.Date.AddMonths(-2) });
            projects.Add(new Project { Id = new Guid("C0991BAE-1DF2-4D01-AC1C-F525004BF092"), Name = "Charles-v1", StartsOn = DateTime.Now.Date.AddMonths(-3) });
            projects.Add(new Project { Id = new Guid("4936767A-B552-4AFF-A6AA-85A98A7A3273"), Name = "Fiyi-v1", StartsOn = DateTime.Now.Date.AddMonths(-3) });
            projects.Add(new Project { Id = new Guid("0A652FA5-396F-4814-A633-FE0697008586"), Name = "Ghost-v1", StartsOn = DateTime.Now.Date.AddMonths(-4) });
            projects.Add(new Project { Id = new Guid("46F2F77D-E912-403C-B326-DA39F2628E08"), Name = "Lebon-v2", StartsOn = DateTime.Now.Date.AddMonths(-4) });
            projects.Add(new Project { Id = new Guid("13C659FB-FA13-4343-BA36-1AF8B48EEE97"), Name = "Liffey-v1", StartsOn = DateTime.Now.Date.AddMonths(-4) });
            projects.Add(new Project { Id = new Guid("54E3A2E3-DBEB-46B2-8BCC-4AB70A9C746A"), Name = "Missouri-v1", StartsOn = DateTime.Now.Date.AddMonths(-5) });
            projects.Add(new Project { Id = new Guid("21DB9D0F-946F-4D2E-8300-E167C20074D3"), Name = "Noble-v3", StartsOn = DateTime.Now.Date.AddMonths(-6) });
            projects.Add(new Project { Id = new Guid("AF06A611-12CE-4C06-B3AD-E2754D12D516"), Name = "ScrumLite-v1", RepositoryUri = "http://svnrepo.scrumlite.org/ScrumLite-v2", StartsOn = DateTime.Now.Date.AddMonths(-2) });
            projects.Add(new Project { Id = new Guid("A0C228BD-4A49-4FCE-BC4D-A72F7B8DDB11"), Name = "R2d2-v1", StartsOn = DateTime.Now.Date.AddMonths(-7) });
            projects.Add(new Project { Id = new Guid("39764E31-F2EB-40B5-88A8-8FBF87CB66F5"), Name = "Volga-v1", StartsOn = DateTime.Now.Date.AddMonths(-8) });
            projects.Add(new Project { Id = new Guid("96585FB8-2FCD-41AE-9807-1C9C4E76FA20"), Name = "Yellowstone-v1", StartsOn = DateTime.Now.Date.AddMonths(-9) });
            projects.Add(new Project { Id = new Guid("1D8EFB61-24CF-49D9-BA26-2BC45E45610C"), Name = "Mj-v2", StartsOn = DateTime.Now.Date.AddMonths(-10) });
            projects.Add(new Project { Id = new Guid("99E32E28-6F92-4a26-ABF1-EE4F56F5BCB6"), Name = "Bonsai-v1", StartsOn = DateTime.Now.Date.AddMonths(-10) });


            //Project Roles
            internalProjectRoles.Add(new ProjectRole() { Id = Guid.NewGuid(), Name = "Developer Sr." });
            internalProjectRoles.Add(new ProjectRole() { Id = Guid.NewGuid(), Name = "PM" });

            externalProjectRoles.Add(new ProjectRole() { Id = Guid.NewGuid(), Name = "Client" });
        }

        public string GetProjectName(string projectId)
        {
            var project = (from p in projects
                           where p.Id == new Guid(projectId)
                           select p).SingleOrDefault();

            if (project != null)
            {
                return project.Name;
            }
            else
            {
                return "Unknown";
            }
        }

        public string GetProjectRepository(string projectId)
        {
            var project = (from p in projects
                           where p.Id == new Guid(projectId)
                           select p).SingleOrDefault();

            if (project != null)
            {
                return project.RepositoryUri;
            }
            else
            {
                return "";
            }
        }
        
        public Project[] GetProjects()
        {
            return projects.ToArray();
        }

        public void Add(Project project)
        {
            project.Id = Guid.NewGuid();
            AddProjectCalled = true;
            this.ProjectAdded = project;
            projects.Add(project);
        }

        public PersonAssignation[] GetPersonsInProject(string projectId)
        {
            Guid guid = new Guid(projectId);

            List<PersonAssignation> pPersons = new List<PersonAssignation>();

            pPersons.Add(new PersonAssignation()
            {
                IsInternal = true,
                Person = new Person() { Id = "johnny", Name = "Johnny Halife", Mail = "johnny@scrumlite.org" },
                Project = new Project() { Id = new Guid(projectId), Name = "ScrumLite" },
                Roles = this.internalProjectRoles.ToArray()
            });

            pPersons.Add(new PersonAssignation()
            {
                IsInternal = true,
                Person = new Person() { Id = "emorito", Name = "Ezequiel Morito", Mail = "ezequiel.morito@scrumlite.org" },
                Project = new Project() { Id = new Guid(projectId), Name = "ScrumLite" },
                Roles = new ProjectRole[] { internalProjectRoles.Find(e => e.Name.ToString().Equals("Developer Sr.")) }
            });

            pPersons.Add(new PersonAssignation()
            {
                IsInternal = false,
                Person = new Person() { Id = "nelly", Name = "Nelly Delgado", Mail = "nelly@microsoft.com" },
                Project = new Project() { Id = new Guid(projectId), Name = "ScrumLite" },
                Roles = externalProjectRoles.ToArray()
            });

            return pPersons.ToArray();
        }

        public string GetUriProjectSpecificMetrics(string projectId)
        {
            var project = (from p in projects
                           where p.Id == new Guid(projectId)
                           select p).SingleOrDefault();

            if (project != null)
            {
                return project.ProjectSpecificMetricsUri;
            }
            else
            {
                return "";
            }
        }

        public Project GetProject(string projectId)
        {
            throw new NotImplementedException(projectId);
        }

        public Project GetProjectByName(string projectName)
        {
            throw new NotImplementedException(projectName);
        }

        public void AddPersonToProject(string projectId, TeamMember person)
        {
            Guid guid = new Guid(projectId);

            if (!teams.ContainsKey(guid))
                teams[guid] = new List<TeamMember>();

            teams[guid].Add(person);
        }

        public void RemovePersonFromProject(string projectId, string personId)
        {
            throw new NotImplementedException();
        }

        public CheckStatus[] CheckCompliance(string projectId)
        {
            CheckStatus[] checkList = null;
            if (projectId.Trim() == "AF06A611-12CE-4C06-B3AD-E2754D12D516")
            {
                checkList = new CheckStatus[1];
                checkList[0] = new CheckStatus();
                checkList[0].Status = false;
                checkList[0].PropertyChecked = "WorkAreas";
                checkList[0].Errors.Add("error1");
            }
            return checkList;
        }

        public void Update(Project project)
        {
            UpdateProjectCalled = true;
            ProjectAdded = project;
        }
    }
}
